package com.yimi.flutter.plugin.flutter_plugin_baidu

import android.content.Context
import androidx.annotation.NonNull
import com.yimi.flutter.plugin.flutter_plugin_baidu.app.YmApp
import com.yimi.flutter.plugin.flutter_plugin_baidu.constant.PluginKey
import com.yimi.flutter.plugin.flutter_plugin_baidu.methodcall.AsrUtil
import com.yimi.flutter.plugin.flutter_plugin_baidu.methodcall.SpeakUtil

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import java.util.HashMap

/** FlutterPluginBaiduPlugin */
class FlutterPluginBaiduPlugin: FlutterPlugin, MethodCallHandler {

  private lateinit var channel : MethodChannel
  private lateinit var mContext: Context

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    //初始化上下文
    mContext = flutterPluginBinding.applicationContext
    YmApp.context = mContext
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_plugin_baidu")
    channel.setMethodCallHandler(this)
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    if (call.method == "getPlatformVersion") {
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
    } else if (call.method == "speech") { //语音播报

      speech(call);
      result.success("000speech")

    } else if (call.method == "start") { //语音播报
      AsrUtil.start(call, result)

    } else if (call.method == "stop") { //语音播报
      AsrUtil.stop()
      result.success("000stop")
    } else if (call.method == "cancel") { //语音播报
      AsrUtil.cancel()
      result.success("000cancel")
    }
    else {
      result.notImplemented()
    }
  }

  private fun speech(call: MethodCall) {

    val speechKey: String? = call.argument(PluginKey.SPEECHKEY)
    SpeakUtil.speak(speechKey)
  }


}
